package com.cjx.admin.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjx.admin.api.AdminResourceService;
import com.cjx.admin.dao.AdminResourceMapper;
import com.cjx.admin.dao.AdminRoleResourceMapper;
import com.cjx.admin.dto.AdminResourceDTO;
import com.cjx.admin.dto.AdminResourceNodeDTO;
import com.cjx.admin.dto.AdminRoleResourceDTO;
import com.cjx.admin.entity.AdminResourceEntity;
import com.cjx.common.api.RedisService;
import com.cjx.common.constant.AuthConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
*@Description 资源服务实现类
*@Verson v1.0.0
*@Author cjunxian
*@Date
*/
@Service
public class AdminResourceServiceImpl extends ServiceImpl<AdminResourceMapper,AdminResourceEntity> implements AdminResourceService {

    @Autowired
    private AdminRoleResourceMapper adminRoleResourceMapper;

    @Autowired
    private AdminResourceMapper adminResourceMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public int deleteByIds(List<String> list) {
        return adminResourceMapper.deleteBatchIds(list);
    }

    @Override
    public Boolean insert(AdminResourceDTO adminResourceDTO) {
        AdminResourceEntity adminResourceEntity=new AdminResourceEntity();
        BeanUtil.copyProperties(adminResourceDTO,adminResourceEntity);
        adminResourceMapper.insert(adminResourceEntity);
        return true;
    }

    @Override
    public Boolean update(AdminResourceDTO adminResourceDTO) {
        AdminResourceEntity adminResourceEntity=new AdminResourceEntity();
        BeanUtil.copyProperties(adminResourceDTO,adminResourceEntity);
        adminResourceMapper.updateById(adminResourceEntity);
        return true;
    }

    @Override
    public AdminResourceDTO selectById(String id) {
        AdminResourceDTO adminResourceDTO=new AdminResourceDTO();
        BeanUtils.copyProperties(adminResourceMapper.selectById(id),adminResourceDTO);
        return adminResourceDTO;
    }

    /**
     * 非父级节点
     */
    private Map<String,List<AdminResourceEntity>> childResources;

    @Override
    public List<AdminResourceNodeDTO> getResourceTree() {
        //查询出全部资源
        List<AdminResourceEntity> allResources = list();
        childResources = allResources.stream().filter(r ->!(ObjectUtil.isNull(r)||"#".equals(r.getParentId()))).collect(Collectors.groupingBy(AdminResourceEntity::getParentId));
        //树
        List<AdminResourceNodeDTO> tree = new ArrayList<>();
        for (AdminResourceEntity resourceEntity : allResources) {
            //取出第一层目录
            if(ObjectUtil.isNull(resourceEntity.getParentId())||"#".equals(resourceEntity.getParentId())){
                AdminResourceNodeDTO top = new AdminResourceNodeDTO();
                top.setId(resourceEntity.getId());
                top.setResourceName(resourceEntity.getResourceName());
                top.setParentId(resourceEntity.getParentId());
                top.setUrl(resourceEntity.getUrl());
                buildChildrenTree(top);
                tree.add(top);
            }
        }
        return tree;
    }

    /**
     * 创建子树
     * @param top
     */
    private void buildChildrenTree(AdminResourceNodeDTO top) {

        List<AdminResourceNodeDTO> childNodes = new ArrayList<>();
        //目录子树
        List<AdminResourceEntity> childResource  = childResources.get(top.getId());
        if(ObjectUtil.isNull(childResource)||0 == childResource.size()){
            return;
        }

        List<String> itemIds = new ArrayList<>();

        for (AdminResourceEntity resourceEntity : childResource) {
            AdminResourceNodeDTO childNode = new AdminResourceNodeDTO();
            childNode.setId(resourceEntity.getId());
            childNode.setUrl(resourceEntity.getUrl());
            childNode.setParentId(resourceEntity.getParentId());
            childNode.setResourceName(resourceEntity.getResourceName());
            buildChildrenTree(childNode);
            if(CollectionUtil.isNotEmpty(childNode.getItemIds())) {
                for (String itemId : childNode.getItemIds()) {
                    itemIds.add(itemId);
                }
            }
            itemIds.add(childNode.getId());
            childNodes.add(childNode);
        }
        top.setItemIds(itemIds);
        top.setChildren(childNodes);
    }

    @Override
    public void initRoleResource() {
        Map<String,Object> resourceMap = new HashMap<>();
        List<AdminRoleResourceDTO> allRoleResource = adminRoleResourceMapper.getAllRoleResource();
        for (AdminRoleResourceDTO roleResourceDTO : allRoleResource) {
            resourceMap.put(roleResourceDTO.getResourceUrl(),AuthConstant.ROLE_KEY+roleResourceDTO.getRoleId());
        }
        //缓存角色资源关系
        redisService.del(AuthConstant.RESOURCE_ROLES_KEY);
        redisService.hSetAll(AuthConstant.RESOURCE_ROLES_KEY,resourceMap);
    }


}
